package com.yanggu.redisson.controller;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/test-cache")
public class TestCacheController {

    @Autowired
    @Qualifier("test-map-cache")
    private RMapCache<String, String> mapCache;

    @Autowired
    @Qualifier("test-set-cache")
    private RSetCache<String> setCache;

    @Autowired
    @Qualifier("test-list-multimap-cache")
    private RListMultimapCache<String, String> listMultimapCache;

    @Autowired
    @Qualifier("test-set-multimap-cache")
    private RSetMultimapCache<String, String> setMultimapCache;

    @Autowired
    @Qualifier("test-local-cached-map")
    private RLocalCachedMap<String, String> rLocalCachedMap;

    //MapCache结构，对应redis的hash数据类型，可以单独设置某个entry的过期时间
    @GetMapping("/test1")
    public void test1() {
        mapCache.put("key1", "value1", 1L, TimeUnit.MINUTES);
    }

    @GetMapping("/test2")
    public void test2() {
        Object value1 = mapCache.get("key1");
        System.out.println("key1 = " + value1);
    }

    //SetCache结构，对应redis的set数据类型，可以单独设置某个元素的过期时间
    @GetMapping("/test3")
    public void test3() {
        setCache.add("value1", 1L, TimeUnit.MINUTES);
    }

    @GetMapping("/test4")
    public void test4() {
        System.out.println("setCache = " + setCache.contains("value1"));
    }

    //ListMultimapCache结构，对应redis的hash数据类型，功能进行了增强，value可以理解为List<V>
    //比较遗憾的是，不支持单个元素的过期时间设置
    @GetMapping("/test5")
    public void test5() {
        boolean put = listMultimapCache.put("key1", "value1");
        System.out.println("put = " + put);

        put = listMultimapCache.put("key1", "value1");
        System.out.println("put = " + put);

        boolean put1 = listMultimapCache.put("key1", "value2");
        System.out.println("put1 = " + put1);
    }

    @GetMapping("/test6")
    public void test6() {
        System.out.println("listMultimapCache = " + listMultimapCache.get("key1"));
    }

    //SetMultimapCache结构，对应redis的hash数据类型，功能进行了增强，value可以理解为Set<V>
    //比较遗憾的是，不支持单个元素的过期时间设置
    @GetMapping("/test7")
    public void test7() {
        boolean put = setMultimapCache.put("key1", "value1");
        System.out.println("put = " + put);

        put = setMultimapCache.put("key1", "value1");
        System.out.println("put = " + put);

        boolean put1 = setMultimapCache.put("key1", "value2");
        System.out.println("put1 = " + put1);
    }

    @GetMapping("/test8")
    public void test8() {
        RSet<String> key1 = setMultimapCache.get("key1");
        for (String s : key1) {
            System.out.println("s = " + s);
        }
    }

    @GetMapping("/test9")
    public void test9() {
        rLocalCachedMap.put("key1", "value1");
    }

    @GetMapping("/test10")
    public void test10() {
        System.out.println("rLocalCachedMap = " + rLocalCachedMap.get("key1"));
    }

}
